<template>
  <div class="containerCon">
    <el-form :model="form" class="login-form">
      <div class="login-wrap">
        <div class="container-title">
          <h3 class="title">用户登录</h3>
        </div>
        <el-form-item>
          <el-input
            v-model="username"
            placeholder="请输入用户名"
            prefix-icon="User"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="password"
            placeholder="请输入密码"
            style="width: 300px"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <div class="login">
          <el-button
            type="primary"
            class="login-button"
            @click="handleLogin"
            style="width: 300px"
            >登录</el-button
          >
        </div>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { getCurrentInstance, ref } from "vue";
import { Edit } from "@element-plus/icons-vue";
import { useRoute, useRouter } from "vue-router";
const { $axios } = getCurrentInstance().appContext.config.globalProperties;
const route = useRoute();
const router = useRouter();
const form = ref();
// 用户名
const username = ref("");
// 密码
const password = ref("");
// 登录按钮;
const handleLogin = () => {
  let token = ref();
  // 请求;
  $axios(
    `/api/login?username=${username.value}&password=${password.value}`,
    "POST"
  ).then((data) => {
    // console.log(data);
    // console.log(data.data.meta);
    // console.log(data.data.data.token);
    if (data.data.meta.status != 200) {
      alert(data.data.meta.msg);
      return;
    }
    localStorage.setItem("token", `${data.data.data.token}`);
    localStorage.setItem("username", `${data.data.data.username}`);
    const destPath = route.query.redirect;
    if (destPath) router.replace(destPath);
    else router.replace("/");
  });
};
</script>

<style lang="less">
.containerCon {
  width: 99vw;
  height: 97vh;
  box-sizing: border-box;
  background: #2d3a4b;
  position: relative;
  .login-form {
    // display: flex;
    // align-items: center;
    .login-wrap {
      position: absolute;
      margin-top: 150px;
      left: 50%;
      transform: translate(-50%);
      // background-color: antiquewhite;
      .container-title {
        h3 {
          text-align: center;
          color: #fff;
        }
      }
    }
  }
}
</style>
